package com.thor.palmmall.impl.pay;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thor.commons.util.Assert;
import com.thor.palmmall.api.pay.chinapay.ChinaPayment;
import com.thor.palmmall.api.pay.chinapay.MutiChannelChinaPayService;
import com.thor.palmmall.rest.user.AChannelContents;
import com.thor.palmmall.util.pay.ChinaPayUtil;

/**
 * 
 * @author liyang
 * 
 * @since 1.0
 *
 */
public class MutiChannelChinaPayServiceImpl implements MutiChannelChinaPayService {
  private static final String signatureField = "Signature";
  private static final Logger logger = LoggerFactory
      .getLogger(MutiChannelChinaPayServiceImpl.class);

  @Override
  public ChinaPayment getChinaPayment(Map<String, Object> params, AChannelContents channel)
      throws Exception {
    Assert.assertArgumentNotNull(params, "params");
    // 初始化银联商务配置
    ChinaPayUtil.initSecssUtil(channel);

    Map<String, Object> contentData = new HashMap<String, Object>();
    contentData.put("Version", "20140728");
    contentData.put("CurryNo", "CNY");
    contentData.put("BusiType", "0001");
    prepareParams(params, contentData, channel);

    ChinaPayUtil.initSecssUtil(channel);
    // 签名
    String signature = ChinaPayUtil.sign(contentData);
    contentData.put(signatureField, signature);
    contentData.put("PayUrl", channel.getAppRequestUrl());
    logger.info(contentData.toString());
    ChinaPayment payment = new ChinaPayment();
    payment.setMerBgUrl((String) contentData.get("MerBgUrl"));
    payment.setTranDate((String) contentData.get("TranDate"));
    payment.setRemoteAddr((String) contentData.get("RemoteAddr"));
    payment.setMerId((String) contentData.get("MerId"));
    payment.setTranTime((String) contentData.get("TranTime"));
    payment.setMerOrderNo((String) contentData.get("MerOrderNo"));
    payment.setCurryNo((String) contentData.get("CurryNo"));
    payment.setVersion((String) contentData.get("Version"));
    payment.setOrderAmt((String) contentData.get("OrderAmt"));
    payment.setSignature((String) contentData.get("Signature"));
    payment.setCommodityMsg((String) contentData.get("CommodityMsg"));
    payment.setPayUrl((String) contentData.get("PayUrl"));
    payment.setBusiType((String) contentData.get("BusiType"));
    payment.setMerPageUrl((String) contentData.get("MerPageUrl"));
    payment.setTranType((String) contentData.get("TranType"));

    // 生成路径
    String redirectUrl = String.format("%s?%s", channel.getAppRequestUrl(),
        getUrlParamsByMap(contentData));
    logger.info(redirectUrl);
    return payment;
  }

  public String getUrlParamsByMap(Map<String, Object> map) {
    if (map == null) {
      return "";
    }
    StringBuffer sb = new StringBuffer();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
      sb.append(entry.getKey() + "=" + entry.getValue().toString());
      sb.append("&");
    }
    String s = sb.toString();
    if (s.endsWith("&")) {
      s = s.substring(0, s.length() - 1);
    }
    return s;
  }

  private void prepareParams(Map<String, Object> params, Map<String, Object> contentData,
      AChannelContents channel) {
    Iterator<Map.Entry<String, Object>> iterator = params.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry<String, Object> entry = iterator.next();
      contentData.put(entry.getKey(), entry.getValue());
    }
    contentData.put("MerPageUrl", channel.getFrontUrl());
    contentData.put("MerBgUrl", channel.getBackUrl());
    contentData.put("MerId", channel.getMerId());

  }
}
